<?php
class HackerU_Server_Rest
{
	
	const ROOT_DIR_DATA = '../../../../HackersU/DbAsXml';
	
	public function __construct($methodName)
	{
		$methodName = 'get'.ucfirst($methodName);
		header("content-type: text/xml");
		if(!method_exists($this, $methodName)){
			$methodName = 'getErrors';
		}
		echo $this->$methodName();
	}
	
	public function getErrors($msg = 'method does not exist')
	{
		$xml = new DOMDocument('1.0', 'utf-8');
		$error = $xml->createElement('error', $msg);
		$xml->appendChild($error);
		return $xml->saveXML();
	}
	
	public function getUsers()
	{
		$filePath = self::ROOT_DIR_DATA.'/users.xml';
		if(!file_exists($filePath)){
			return $this->getErrors('file users.xml does not exist');
		}
		return file_get_contents(self::ROOT_DIR_DATA.'/users.xml');
	}
	
	public function getTests()
	{
		$filePath = self::ROOT_DIR_DATA.'/tests.xml';
		if(!file_exists($filePath)){
			return $this->getErrors('file tests.xml does not exist');
		}
		return file_get_contents(self::ROOT_DIR_DATA.'/tests.xml');
	}
}

new HackerU_Server_Rest($_REQUEST['func']);